package org.darkforest.common.services.impl;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.services.SpecialityService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.SpecialityGoodsMapper;
import org.darkforest.dao.mapper.SpecialityMapper;
import org.darkforest.model.Speciality;
import org.darkforest.model.SpecialityGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by Administrator on 2017/4/11.
 */
@Service
public class SpecialityServiceImpl implements SpecialityService {


    @Autowired
    private SpecialityMapper specialityMapper;

    @Autowired
    private SpecialityGoodsMapper specialityGoodsMapper;

    /**
     * 新增活动 以及活动关联的商品
     *
     * @param speciality
     * @return
     */
    @Override
    @Transactional
    public boolean doAddSpeciality(Speciality speciality) {
        specialityMapper.insertSelective(speciality);
        if(speciality.getSpecialityGoodsList()!=null){
            for(SpecialityGoods specialityGoods:speciality.getSpecialityGoodsList()){
                if(StringUtils.isNotBlank(specialityGoods.getsGoodsName())){
                    specialityGoods.setCreateTime(DateUtil.getCurrentDateTime());
                    specialityGoods.setsImage(speciality.getSpecialityImage());
                    specialityGoods.setsSpecialityId(speciality.getSpecialityId());
                    specialityGoods.setsGoodsState(1);
                    specialityGoodsMapper.insertSelective(specialityGoods);
                }
            }
        }
        return true;
    }

    /**
     * 编辑活动 以及修改活动关联的商品
     *
     * @param speciality
     * @return
     */
    @Override
    public boolean doEditSpeciality(Speciality speciality) {
        specialityMapper.updateByPrimaryKeySelective(speciality);
        specialityGoodsMapper.deleteBySpecialityId(speciality.getSpecialityId());
        if(speciality.getSpecialityGoodsList()!=null){
            for(SpecialityGoods specialityGoods:speciality.getSpecialityGoodsList()){
                if(StringUtils.isNotBlank(specialityGoods.getsGoodsName())){
                    specialityGoods.setCreateTime(DateUtil.getCurrentDateTime());
                    specialityGoods.setsImage(speciality.getSpecialityImage());
                    specialityGoods.setsSpecialityId(speciality.getSpecialityId());
                    specialityGoods.setsGoodsState(1);
                    specialityGoodsMapper.insertSelective(specialityGoods);
                }
            }
        }

        return true;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean delSpeciality(Integer[] ids) {

        for(Integer id:ids){
            specialityMapper.deleteByPrimaryKey(id);
            specialityGoodsMapper.deleteBySpecialityId(id);
        }

        return true;
    }
}
